package com.community.process._1010.common.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.Map;

@Slf4j
public class HttpUtils extends HttpUtil {
    /**
     * JSON http 请求
     * @param url url
     * @param param 参数
     * @param headers 请求头
     * @param timeoutSeconds 超时时间
     * @return String
     */
    public static String postToJson(String url, Map<String, Object> param, Map<String, String> headers,int timeoutSeconds) {
        HttpRequest http = createPost(url);
        String body = getJsonHttp(param, headers, timeoutSeconds, http);
        log.info("postToJson url:{},请求返回body：{}",url,body);
        return body;
    }

    /**
     *  http 请求
     * @param url url
     * @param param 参数
     * @param headers 请求头
     * @param timeoutSeconds 超时时间
     * @return String
     */
    public static String post(String url, Map<String, Object> param, Map<String, String> headers,int timeoutSeconds) {
        HttpRequest http = createPost(url);
        String body = getHttp(param, headers, timeoutSeconds, http);
        log.info("post url:{},请求返回body：{}",url,body);
        return body;
    }

    /**
     *  http 请求
     * @param url url
     * @param param 参数
     * @param headers 请求头
     * @param timeoutSeconds 超时时间
     * @return String
     */
    public static String get(String url, Map<String, Object> param, Map<String, String> headers,int timeoutSeconds) {
        HttpRequest http = createGet(url);
        String body = getHttp(param, headers, timeoutSeconds, http);
        log.info("get url:{},请求返回body：{}",url,body);
        return body;
    }

    /**
     *  http 请求
     * @param url url
     * @param param 参数
     * @param headers 请求头
     * @param timeoutSeconds 超时时间
     * @return String
     */
    public static String put(String url, Map<String, Object> param, Map<String, String> headers,int timeoutSeconds) {
        HttpRequest http = createRequest(Method.PUT,url);
        String body = getHttp(param, headers, timeoutSeconds, http);
        log.info("put url:{},请求返回body：{}",url,body);
        return body;
    }

    private static String getJsonHttp(Map<String, Object> param, Map<String, String> headers, int timeoutSeconds, HttpRequest http) {
        if (!CollectionUtils.isEmpty(headers)) {
            headers.forEach(http::header);
        }
        if (!CollectionUtils.isEmpty(param)) {
            http.body(JSON.toJSONString(param));
        }
        if (timeoutSeconds > 0) {
            http.timeout(timeoutSeconds * 1000);
        }
        return http.execute().body();
    }

    private static String getHttp(Map<String, Object> param, Map<String, String> headers, int timeoutSeconds, HttpRequest http) {
        if (!CollectionUtils.isEmpty(headers)) {
            http.addHeaders(headers);
        }
        if (!CollectionUtils.isEmpty(param)) {
            http.form(param);
        }
        if (timeoutSeconds > 0) {
            http.timeout(timeoutSeconds * 1000);
        }
        return http.execute().body();
    }
}
